package com.woniuxy.repay.entity;

import java.io.Serializable;
import java.util.List;

/**
 * @author shichen
 * @create 2021-11-18-18:01
 * @description 封装 page 对象
 */



public class PageBean<T> implements Serializable {
    /**
     * 总数据多少条
     */
    private int total;
    /**
     * 每页数据条数
     */
    private int PAGE_SIZE;
    /**
     * 当前所在页数
     */
    private int pageNumber;
    /**
     * 总共的页数
     */
    private int totalPage;
    /**
     * 分页的开始索引
     */
    private int startIndex;
    /**
     * 分页的结束索引
     */
    private int endIndex;
    /**
     * 查到的结果
     */
    private List<T> products;


    public PageBean(int total, int PAGE_SIZE, int currentPage, List<T> products) {
        super();
        this.total = total;
        this.PAGE_SIZE = PAGE_SIZE;
        this.pageNumber = currentPage;
        this.products = products;
        int tp = setTotalPage(total, PAGE_SIZE);
        setStartAndEndIndex(currentPage,tp);

    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPAGE_SIZE() {
        return PAGE_SIZE;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    /**
     * 在设置当前页面的时候就把 开始 和 结束 索引设置好
     *
     * @param pageNumber
     */
    public void setPageNumber(int pageNumber,int total) {
        this.pageNumber = pageNumber;
        setStartAndEndIndex(pageNumber,total);
    }

    public int getTotalPage() {
        return totalPage;
    }

    /**
     * 在构造该类的时候就，设置总页数
     *
     * @param total
     * @param pageSize
     */
    private int setTotalPage(int total, int pageSize) {
        int ttotalPage = (int) Math.ceil(total / pageSize)+1;
        if (total > 0 && ttotalPage == 0) {
            ttotalPage = 1;
        }
        this.totalPage = ttotalPage;
        return  ttotalPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public int getEndIndex() {
        return endIndex;
    }

    /**
     * 设置开始和结束索引
     *
     * @param pageNumber
     */

    private void setStartAndEndIndex(int pageNumber,int totalPage) {

        if (pageNumber < 10) {
            endIndex = 10;
            startIndex = 1;
        } else {
            startIndex = pageNumber - 4;
            endIndex = pageNumber + 5;
        }
        if (endIndex > totalPage) {
            endIndex = totalPage;
        }

    }

    public List<T> getProducts() {
        return products;
    }

    public void setProducts(List<T> products) {
        this.products = products;
    }

    @Override
    public String toString() {
        return "PageBean [total=" + total + ", PAGE_SIZE=" + PAGE_SIZE + ", pageNumber=" + pageNumber + ", totalPage=" + totalPage + ", startIndex=" + startIndex + ", endIndex=" + endIndex + ", products=" + products + "]";
    }
}
